package com.example.demo_spring.aop.rent.spring.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
//@Component
public class Performance {
//    @Pointcut("execution(* com.example.demo_spring.aop.rent.Landlord.*())")
    @Pointcut("@annotation(com.example.demo_spring.aop.rent.spring.annotation.Performance)")
    public void lService() {}

//    @Around("lService()")
    @Around("execution(* com.example.demo_spring.aop.rent.Landlord.service())")
    public void around(ProceedingJoinPoint point){
        long startTime = System.currentTimeMillis();

        try {
            point.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        String className = point.getTarget().getClass().getName();
        String methodName = point.getSignature().getName();

        long endTime = System.currentTimeMillis();
        long timeCost = endTime - startTime;
        System.out.printf("[%s.%s] time cost: %d ms\n", className, methodName, timeCost);
    }
}
